package com.cn.customer.housekeeping.housekeepingc.ui;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;

import com.cn.customer.housekeeping.housekeepingc.R;
import com.cn.customer.housekeeping.housekeepingc.bean.BaseResp;
import com.cn.customer.housekeeping.housekeepingc.bean.ServicekindResp;
import com.cn.customer.housekeeping.housekeepingc.biz.personcenter.IUserLoginView;
import com.cn.customer.housekeeping.housekeepingc.biz.personcenter.InterfacePresenter;
import com.cn.customer.housekeeping.housekeepingc.constant.Constants;
import com.cn.customer.housekeeping.housekeepingc.constant.URLUtil;
import com.cn.customer.housekeeping.housekeepingc.ui.adapter.CNKFixedPagerAdapter;
import com.cn.customer.housekeeping.housekeepingc.ui.base.BaseFragmentActivity;
import com.cn.customer.housekeeping.housekeepingc.ui.fragment.OrderListActivity;
import com.cn.customer.housekeeping.housekeepingc.util.ToastUtil;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

/***
 * 订单列表
 */
public class OrderFragmentListActivity extends BaseFragmentActivity implements IUserLoginView {
    private InterfacePresenter mInterfacePresenter;
    //    private String[] titles = new String[]{"全部", "氪TV", "O2O", "新硬件", "Fun!!", "企业服务", "Fit&Health", "在线教育", "互联网金融", "大公司", "专栏", "新产品"};
    private TabLayout tab_layout;
    private ViewPager info_viewpager;
    private List<Fragment> fragments;
    private CNKFixedPagerAdapter mPagerAdater;
    private String type, cityname,title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_order_list);
        super.onCreate(savedInstanceState);


    }


    @Override
    public void initViews() {
        mInterfacePresenter = new InterfacePresenter();
        mInterfacePresenter.attachView(OrderFragmentListActivity.this);
        tab_layout = (TabLayout) findViewById(R.id.tab_layout);
        info_viewpager = (ViewPager) findViewById(R.id.info_viewpager);
    }

    @Override
    public void initListeners() {

    }


    @Override
    public void initData() {
        type = getIntent().getExtras().getString(Constants.type);
        cityname = getIntent().getExtras().getString(Constants.cityname);
        title = getIntent().getExtras().getString(Constants.title);
        tvTopTitle.setText(title);
        ToastUtil.makeText(OrderFragmentListActivity.this, type);

        mInterfacePresenter.getSubServiceKind(type, cityname);


    }

    @Override
    public void onError(BaseResp s) {

    }

    ServicekindResp mServicekindResp;

    @Override
    public void onSuccess(BaseResp s) {
        if (s.getUrl().equals(URLUtil.url_get_getSubServiceKind)) {//请求服务子类型
            Gson gson = new Gson();
            mServicekindResp = gson.fromJson(s.getData(), ServicekindResp.class);
            fragments = new ArrayList<>();
            if (mServicekindResp != null && mServicekindResp.getServiceList() != null && mServicekindResp.getServiceList().size() > 0) {
                for (int i = 0; i < mServicekindResp.getServiceList().size(); i++) {

                    String subtype=mServicekindResp.getServiceList().get(i).getSubtype()+"";
                    OrderListActivity oneFragment = new OrderListActivity().  newInstance(type,subtype,cityname);
//                    oneFragment.setArguments(bundle);
                    fragments.add(oneFragment);
                }
                mPagerAdater = new CNKFixedPagerAdapter(getSupportFragmentManager());
                mPagerAdater.setTitles(mServicekindResp.getServiceList());
                mPagerAdater.setFragments(fragments);
                info_viewpager.setAdapter(mPagerAdater);
                tab_layout.setupWithViewPager(info_viewpager);
                tab_layout.setTabMode(TabLayout.MODE_SCROLLABLE);
                for (int i = 0; i < mServicekindResp.getServiceList().size(); i++) {
                    TabLayout.Tab tab = tab_layout.getTabAt(i);
                    tab.setCustomView(mPagerAdater.getTabView(i));
                }
            }
        }


    }

    @Override
    public void setHeader() {
        super.setHeader();
        rlTitle.setVisibility(View.VISIBLE);
        tvTopTitle.setText("");
    }


}
